/*
 * Created on 2007-12-29
 *
 * TODO 
 */
package jacky.lanlan.song.extension.struts.mock;

import jacky.lanlan.song.extension.struts.Scope;
import jacky.lanlan.song.extension.struts.action.ActionContext;
import jacky.lanlan.song.extension.struts.action.TestFormBean;
import jacky.lanlan.song.extension.struts.action.ValueStack;
import jacky.lanlan.song.extension.struts.annotation.Execution;
import jacky.lanlan.song.extension.struts.annotation.FailureTo;
import jacky.lanlan.song.extension.struts.annotation.Form;
import jacky.lanlan.song.extension.struts.annotation.Forward;
import jacky.lanlan.song.extension.struts.annotation.Input;
import jacky.lanlan.song.extension.struts.annotation.POJOAction;
import jacky.lanlan.song.extension.struts.annotation.SuccessTo;
import jacky.lanlan.song.extension.struts.util.Assert;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@POJOAction
//这个 Action 是在另一个包
public class POJOTestActionInOtherPack{

	@Execution
	@Form(type=TestFormBean.class,validate=true)
	@Input("/login.jsp")
	public String formValidateError(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(false, "永远不可能运行到这里！！！");
		return Forward.SUCCESS;
	}
	
	@Execution
	@Form
	@Input("/login.jsp")
	@Forward(name={"success"}, 
			path={"/BBB.jsp"},
			redirect= {false})
	@SuccessTo(path = "/successTo.jsp")//覆盖上面的Forward
	public String executionBusinessMethod(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(input.get("aaa").equals("string value"));
		Assert.isTrue(Integer.parseInt(input.get("bbb"))==111);
		
		Assert.isTrue(vs.getRequestParameter("param").equals("value_reqParam"));

		vs.setAttribute("ccc", "out_request",Scope.Request);
		vs.setAttribute("ddd", "out_session",Scope.Session);
		
		Assert.isTrue(vs instanceof ActionContext);
		ActionContext ac=(ActionContext) vs;
		
		Object value=ac.getContext();
		Assert.notNull(value);
		Assert.isTrue(ServletContext.class.isAssignableFrom(value.getClass()));
		
		value=ac.getRequest();
		Assert.notNull(value);
		Assert.isTrue(HttpServletRequest.class.isAssignableFrom(value.getClass()));

		value=ac.getResponse();
		Assert.notNull(value);
		Assert.isTrue(HttpServletResponse.class.isAssignableFrom(value.getClass()));
		
		return Forward.SUCCESS;
	}
	
	@Execution
	@Form
	@Input("/login.jsp")
	@Forward(name={"success"}, 
			path={"/AAA.jsp"},
			redirect= {false})
	@FailureTo(path="/failureTo.jsp")
	public String testExecutionMethod2(ValueStack vs,Map<String,String> input ) throws Exception {
		Assert.isTrue(input.get("aaa").equals("string value2"));
		Assert.isTrue(Integer.parseInt(input.get("bbb"))==222);
		Assert.isTrue(vs.getRequestParameter("param").equals("value_reqParam2"));

		vs.setAttribute("ccc", "out_request2",Scope.Request);
		vs.setAttribute("ddd", "out_session2",Scope.Session);
		
		Assert.isTrue(vs instanceof ActionContext);
		ActionContext ac=(ActionContext) vs;
		
		Object value=ac.getContext();
		Assert.notNull(value);
		Assert.isTrue(ServletContext.class.isAssignableFrom(value.getClass()));
		
		value=ac.getRequest();
		Assert.notNull(value);
		Assert.isTrue(HttpServletRequest.class.isAssignableFrom(value.getClass()));

		value=ac.getResponse();
		Assert.notNull(value);
		Assert.isTrue(HttpServletResponse.class.isAssignableFrom(value.getClass()));
		
		return Forward.FAILURE;
	}

	
}
